package com.yvan.codeslave.batch;

import com.fasterxml.jackson.databind.JsonNode;
import com.yvan.platform.FileUtils;
import com.yvan.platform.Settings;
import com.yvan.platform.XmlUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.val;

import java.io.File;

@Getter
@Setter
public class TemplateSetting {

    /**
     * 模版名称
     */
    private String name;

    /**
     * 模版相对路径
     */
    private String templateBasePath;

    /**
     * 模版中包含的 vm 文件
     */
    private String[] items;

    /**
     * 基类 XML
     */
    private String[] supers;

    private JsonNode jsonNode;

    /**
     * 初始化模版的基类
     */
    public void init(Settings settings) {
        File[] fs = new File[supers.length];
        int i = 0;
        for (String superFile : supers) {
            val filePath = FileUtils.normalizePath(settings.getTemplateBase(), superFile);
            File file = new File(filePath);
            if (!file.exists() || !file.isFile()) {
                throw new RuntimeException("没有找到基类文件:" + superFile);
            }
            fs[i] = file;
            i++;
        }
        jsonNode = XmlUtils.readXmlToNode(fs);
    }
}
